package com.wsx.sky.gateway.outter.dubbo;

import com.wsx.sky.dubbo.bean.DubboRequest;
import com.wsx.sky.dubbo.bean.DubboResponse;
import com.wsx.sky.dubbo.service.DubboClientService;
import com.wsx.sky.gateway.bean.GateData;
import com.wsx.sky.gateway.bean.GateParam;
import com.wsx.sky.gateway.outter.BaseController;
import org.apache.skywalking.apm.toolkit.trace.TraceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author wangshuaixin
 * @Title: com.wsx.sky.gateway.outter.dubbo.DubboController
 * @Description: TODO
 * @date 2018/12/11
 */
@RestController
public class DubboController extends BaseController {

    @Autowired
    private DubboClientService dubboClientService;

    @RequestMapping(
            value = "/get/dubbo/dubbo/{name}",
            method = {RequestMethod.GET}
    )
    public GateData dubbos(@PathVariable("name") String name) {
        String traceid = TraceContext.traceId();
        System.out.println(traceid);
        if (StringUtils.isEmpty(name)) {
            return new GateData(30001);
        }
        DubboRequest request = new DubboRequest();
        request.setName(name);
        request.setDate(new Date());
        request.setTraceid(traceid);
        DubboResponse dubboResponse = dubboClientService.dubbo(request);
        if (null != dubboResponse) {
            return new GateData().buildDatas(dubboResponse.getMsg());
        }
        return new GateData(50001);
    }


    @RequestMapping(
            value = "/get/dubbo/http/{name}",
            method = {RequestMethod.GET}
    )
    public GateData dubboh(@PathVariable("name") String name) {
        String traceid = TraceContext.traceId();
        System.out.println(traceid);
        if (StringUtils.isEmpty(name)) {
            return new GateData(30001);
        }
        DubboRequest request = new DubboRequest();
        request.setName(name);
        request.setDate(new Date());
        request.setTraceid(traceid);
        DubboResponse dubboResponse = dubboClientService.http(request);
        if (null != dubboResponse) {
            return new GateData().buildDatas(dubboResponse.getMsg());
        }
        return new GateData(50001);
    }

    @RequestMapping(
            value = "/post/dubbo/complex",
            method = {RequestMethod.POST}
    )
    public GateData dubboh(@RequestBody GateParam gateParam) {
        String traceid = TraceContext.traceId();
        System.out.println(traceid);
        if (null == gateParam
                || StringUtils.isEmpty(gateParam.getName())) {
            return new GateData(30001);
        }
        DubboRequest request = new DubboRequest();
        request.setName(gateParam.getName());
        request.setDate(new Date());
        request.setTraceid(traceid);
        DubboResponse dubboResponse = dubboClientService.complex(request);
        if (null != dubboResponse) {
            return new GateData().buildDatas(dubboResponse.getMsg());
        }
        return new GateData(50001);
    }
}
